package com.sicheng.lc.周赛.分类.哈西表or计数or滑动窗口or双指针.计数;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/19 16:58
 */
public class 多个数组求交集 {
    public List<Integer> intersection(int[][] nums) {

        int[] st = new int[1001];
        for (int[] num : nums) {
            for (int x : num) {
                st[x]++;
            }
        }

        ArrayList<Integer> res = new ArrayList<>();
        for (int i = 0; i < st.length; i++) {
            if (st[i] == nums.length) {
                res.add(i);
            }
        }
        return res;
    }

    private ArrayList<Integer> hash(int[][] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int[] num : nums) {
            for (int x : num) {
                map.put(x, 1 + map.getOrDefault(x, 0));
            }
        }

        ArrayList<Integer> list = new ArrayList<>();
        for (int x : map.keySet()) {
            if (map.get(x) == nums.length) {
                list.add(x);
            }
        }
        list.sort(Comparator.naturalOrder());
        return list;
    }
}
